package com.yudean.esb.service.impl.mds;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yudean.esb.domain.mds.MdsLnplnData;
import com.yudean.esb.mapper.mds.MdsLnplnDataMapper;
import com.yudean.esb.service.MdsLnplnDataService;
import com.yudean.esb.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("mdsLnplnDataService")
public class MdsLnplnDataServiceImpl  extends ServiceImpl<MdsLnplnDataMapper, MdsLnplnData> implements MdsLnplnDataService {
    @Resource
    private MdsLnplnDataMapper mdsLnplnDataMapper;


    @Override
    public String getLnFinDataByPage(Map<String,String> map, long pageNoInt, long pageSizeInt,String formName,String accountName) {
        //查询
        QueryWrapper<MdsLnplnData> wrapper = new QueryWrapper<>();
        map.forEach(wrapper::eq);
        if (!StringUtil.isEmpty(formName)){
            wrapper.like("FORMNAME",formName);
        }
        if(!StringUtil.isEmpty(accountName)){
            wrapper.like("ACCOUNTNAME",accountName);
        }
        Page<MdsLnplnData> page = new Page<>(pageNoInt,pageSizeInt);
        mdsLnplnDataMapper.selectPage(page,wrapper);
        List<MdsLnplnData> records = page.getRecords();
        //总数
        long total = page.getTotal();
        if(records.size() == 0){
            return noData();
        }
        return parseLnPlnDataList(records,String.valueOf(total),String.valueOf(pageNoInt),String.valueOf(pageSizeInt));
    }


    public String parseLnPlnDataList(List<MdsLnplnData> list,String total,String pageNoInt,String pageSizeInt){
        Document doc = null;
        doc = DocumentHelper.createDocument();
        Element root = doc.addElement("service");
        root.addElement("name").setText("ERP财务报表信息");
        root.addElement("type").setText("发布服务");
        if(!StringUtil.isEmpty(total) && !StringUtil.isEmpty(pageNoInt) && !StringUtil.isEmpty(pageSizeInt)){
            root.addElement("total").setText(total);
            root.addElement("pageNo").setText(pageNoInt);
            root.addElement("pageSize").setText(pageSizeInt);
        }
        Element dataList = root.addElement("datalist");
        try {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                MdsLnplnData finIndex = list.get(i);
                if (finIndex == null) {
                    continue;
                }
                Element data = dataList.addElement("data");
                this.dataWrapper(finIndex,data);
            }
            return doc.asXML();
        } catch (Exception e) {
            log.error("parseList():error:" + e.getMessage(), e);
            return noData();
        }
    }

    /**
     * 将对象装配到Elment中去
     * @param plnIndex
     * @param data
     */
    private  void dataWrapper(MdsLnplnData plnIndex, Element data) {
        data.addElement("formId").setText(
                StringUtil.convertToString(plnIndex.getFormid()));
        data.addElement("formName").setText(
                StringUtil.convertToString(plnIndex.getFormname()));
        data.addElement("accountId").setText(
                StringUtil.convertToString(plnIndex.getAccountid()));
        data.addElement("accountName").setText(
                StringUtil.convertToString(plnIndex.getAccountname()));
        data.addElement("organisationElementId").setText(
                StringUtil.convertToString(plnIndex.getOrganisationelementid()));
        data.addElement("orgnazationCode").setText(
                StringUtil.convertToString(plnIndex.getOrgnazationcode()));
        data.addElement("organisationElementName").setText(
                StringUtil.convertToString(plnIndex.getOrganisationelementname()));
        data.addElement("period").setText(
                StringUtil.convertToString(plnIndex.getPeriod()));
        data.addElement("currency").setText(
                StringUtil.convertToString(plnIndex.getCurrency()));
        data.addElement("dataLevel").setText(
                StringUtil.convertToString(plnIndex.getDatalevel()));
        data.addElement("adjustmentLevel").setText(
                StringUtil.convertToString(plnIndex.getAdjustmentlevel()));
        data.addElement("valueType").setText(
                StringUtil.convertToString(plnIndex.getValuetype()));
        data.addElement("dataValue").setText(
                StringUtil.convertToString(plnIndex.getDatavalue()));
        data.addElement("creationDate").setText(
                StringUtil.convertToString(DateUtil.formatDate(plnIndex.getCreationdate())));
    }


    /**
     * 返回空数据的时候的信息
     *
     * @return
     */
    private static String noData() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>ERP财务报表信息</name><type>发布服务</type><datalist><!--查无数据--></datalist></service>";
    }
}
